// 引入
const express = require('express');
const db = require('../../utils/db')

// 配置路由
const sheBaoRoter = express.Router();

//编写接口
//接收前端数据(渲染页面)
sheBaoRoter.all('/xinchou/gettwo', (req, res) => {
    const page = req.query.page;
    const count = req.query.count;
    console.log(res)
    const sql = `SELECT * FROM provident LIMIT ${(page - 1) * count},${count};`;
    const sql2=`SELECT COUNT(*) as total from provident;`;
    db.query(sql, function (err, data) {
        if (err) {
            res.send({ error: 1, msg: err })
        } else {
            // res.send({ error: 0, msg: '添加成功', data: data });
            db.query(sql2, (err2, data2) => {
                if (err2) {
                    console.log(err2)
                    res.send({ error: 1, msg: err2 })
                } else {
                    console.log(data2)
                    res.send({ error: 0, msg: '查询成功', data: data, total: data2[0].total })
                }
            })
        }
    })
});
// 删除
sheBaoRoter.post('/xinchou/deltwo', (req, res) => {
    const sql = 'delete from provident where p_id="' + req.body.id + '";';
    db.query(sql, (err, data) => {
        if (err) {
            console.log(err)
            res.send({ error: 1, msg: err })
        } else {
            res.send({ error: 0, msg: '删除成功' })
        }
    })
})
// // 查询
sheBaoRoter.post('/xinchou/inquiretwo', (req, res) => {
    const sql = `SELECT * FROM provident WHERE plan_id='${req.body.plan_id}' OR plan_name='${req.body.plan_name}' OR p_effDate='${req.body.p_effDate}';`
    db.query(sql, (err, data) => {
        if (err) {
            console.log(err)
            res.send({ error: 1, msg: err })
        }else {
            res.send({ error: 0, msg: '查询成功' ,data: data})
        }
    })
})

// // 新增
sheBaoRoter.all('/xinchou/addtwo', (req, res) => {
    console.log(1);
    const sql =`INSERT INTO provident VALUES(NULL,'${req.body.plan_id}','${req.body.plan_name}','${req.body.p_effDate}','${req.body.p_expDate}','${req.body.p_crePerson}','${req.body.p_creDate}','${req.body.p_update}');`
    console.log(req.body);
    db.query(sql, (err, data) => {
        if (err) {
            console.log(err)
            res.send({ error: 1, msg: err })
        }else {
            res.send({ error: 0, msg: '添加成功',data:data})
        }
    })
})

// // 导出
module.exports = sheBaoRoter;

//编辑
sheBaoRoter.post('/xinchou/edittwo', (req, res) => {
    const sql = `UPDATE provident SET plan_name='${req.body.plan_name}',p_effDate='${req.body.p_effDate}',p_expDate='${req.body.p_expDate}',p_crePerson='${req.body.p_crePerson}',p_creDate='${req.body.p_creDate}',p_update='${req.body.p_update}' WHERE plan_id='${req.body.plan_id}';`;
    db.query(sql, (err, data) => {
        if (err) {
            console.log(err)
            res.send({ error: 1, msg: err })
        } else {
            res.send({ error: 0, msg: '修改成功' ,data:data})
        }
    })
});
